package com.caiheng.api.service.estate.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caiheng.api.dao.estate.EstateRepairAssistMapper;
import com.caiheng.api.dao.sys.SysDeptMapper;
import com.caiheng.api.dao.sys.SysUserMapper;
import com.caiheng.api.dao.wxuser.UserMapper;
import com.caiheng.api.entity.estate.EstateRepairAssist;
import com.caiheng.api.entity.sys.SysDept;
import com.caiheng.api.entity.sys.SysUser;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.exception.TransactionalException;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import com.caiheng.api.service.estate.EstateRepairAssistService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 报修的协助人员Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-18
 */
@Service
public class EstateRepairAssistServiceImpl extends BaseServiceImpl<EstateRepairAssistMapper, EstateRepairAssist> implements EstateRepairAssistService {

    @Resource
    private EstateRepairAssistMapper assistMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysDeptMapper sysDeptMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    @Transactional
    public int addAssist(int recordId, JSONArray sysUserIds) {
        int ret = 0;
        try{

            List<Long> ids = JSONUtil.toList(sysUserIds, Long.class);

            List<Long> deleteIds = new ArrayList<>();
            List<EstateRepairAssist> oldList = assistMapper.selectListByColumn("record_id",recordId);
            if(CollUtil.isNotEmpty(oldList)){
                for (EstateRepairAssist repairAssist : oldList){
                    if(!ids.contains(repairAssist.getSysUserId())){
                        deleteIds.add(repairAssist.getId());
                    }
                }
            }



            if(CollUtil.isNotEmpty(ids)){
                for (Long id : ids){
                    QueryWrapper<EstateRepairAssist> qw = new QueryWrapper<>();
                    qw.eq("record_id", recordId);
                    qw.eq("sys_user_id", id);
                    qw.last("limit 1");
                    EstateRepairAssist recordAssist = assistMapper.selectOne(qw);
                    if(recordAssist == null){

                        recordAssist = new EstateRepairAssist();
                        SysUser sysUser = sysUserMapper.selectById(id);
                        if(sysUser!= null){
                            recordAssist.setDeptId(sysUser.getDeptId());
                            recordAssist.setName(sysUser.getNickName());

                            SysDept sysDept = sysDeptMapper.selectById(sysUser.getDeptId());
                            if(sysDept != null){
                                recordAssist.setDeptName(sysDept.getDeptName());
                            }

                            recordAssist.setPhoneNum(sysUser.getPhoneNum());
                        }
                        recordAssist.setRecordId(recordId);
                        recordAssist.setSysUserId(id);
                        recordAssist.setCreateTime(new Date());

                        QueryWrapper<User> qwUser = new QueryWrapper<>();
                        qwUser.eq("sys_user_id", id);
                        qwUser.last("limit 1");
                        User userAssist = userMapper.selectOne(qwUser);
                        if(userAssist != null){
                            recordAssist.setUserId(userAssist.getId());
                        }
                        ret = assistMapper.insert(recordAssist);
                        if(ret <= 0){
                            throw new TransactionalException("assistMapper insert error");
                        }
                    }
                }
            }
            for (Long deleteId : deleteIds){
                ret = assistMapper.deleteById(deleteId);
                if(ret <= 0){
                    throw new TransactionalException("assistMapper delete error");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("msgMapper insert error");
        }
        return ret;
    }
}
